#!/usr/bin/env python3
"""
rubygem-redcarpet MCP Server

面向 rubygem-redcarpet 软件的 MCP Server，提供 redcarpet 命令行工具的包装功能。

MCP tools 列表：
- redcarpet_help: 显示 redcarpet 使用帮助
- redcarpet_version: 显示 redcarpet 版本信息  
- redcarpet_convert: 将 Markdown 文件转换为 HTML
- redcarpet_smarty: 使用 SmartyPants 转换 Markdown 文件
- redcarpet_parse: 使用指定解析器扩展转换 Markdown 文件
- redcarpet_render: 使用指定渲染器选项转换 Markdown 文件
"""

import subprocess
import json
from typing import List, Optional
from mcp.server.fastmcp import FastMCP, Context

mcp = FastMCP("Redcarpet MCP Server")

@mcp.tool()
def redcarpet_help(help_option: str = "-h") -> dict:
    """
    显示 redcarpet 命令行工具的使用帮助
    
    Args:
        help_option: 帮助选项，可以是 "-h" 或 "--help"
    
    Returns:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["redcarpet", help_option],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": f"redcarpet {help_option}",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": f"redcarpet {help_option}",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def redcarpet_version(version_option: str = "-v") -> dict:
    """
    显示 redcarpet 命令行工具的版本信息
    
    Args:
        version_option: 版本选项，可以是 "-v" 或 "--version"
    
    Returns:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["redcarpet", version_option],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": f"redcarpet {version_option}",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": f"redcarpet {version_option}",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def redcarpet_convert(markdown_file: str) -> dict:
    """
    将 Markdown 文件转换为 HTML
    
    Args:
        markdown_file: 要转换的 Markdown 文件路径
    
    Returns:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["redcarpet", markdown_file],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": f"redcarpet {markdown_file}",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": f"redcarpet {markdown_file}",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def redcarpet_smarty(markdown_file: str) -> dict:
    """
    使用 SmartyPants 转换 Markdown 文件（将引号、破折号等转换为印刷体）
    
    Args:
        markdown_file: 要转换的 Markdown 文件路径
    
    Returns:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["redcarpet", "--smarty", markdown_file],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": f"redcarpet --smarty {markdown_file}",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": f"redcarpet --smarty {markdown_file}",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def redcarpet_parse(markdown_file: str, parse_option: str) -> dict:
    """
    使用指定解析器扩展转换 Markdown 文件
    
    Args:
        markdown_file: 要转换的 Markdown 文件路径
        parse_option: 解析器选项，如 "highlight" 用于语法高亮
    
    Returns:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["redcarpet", "--parse", parse_option, markdown_file],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": f"redcarpet --parse {parse_option} {markdown_file}",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": f"redcarpet --parse {parse_option} {markdown_file}",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def redcarpet_render(markdown_file: str, render_option: str) -> dict:
    """
    使用指定渲染器选项转换 Markdown 文件
    
    Args:
        markdown_file: 要转换的 Markdown 文件路径
        render_option: 渲染器选项，如 "no-links" 用于禁用链接渲染
    
    Returns:
        dict: 包含命令执行结果的统一 JSON 结构
    """
    try:
        result = subprocess.run(
            ["redcarpet", "--render", render_option, markdown_file],
            capture_output=True,
            text=True,
            timeout=30
        )
        return {
            "success": result.returncode == 0,
            "command": f"redcarpet --render {render_option} {markdown_file}",
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except Exception as e:
        return {
            "success": False,
            "command": f"redcarpet --render {render_option} {markdown_file}",
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()